package com.ymttest.business.service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;

import com.ymatou.app.api.parameter.AppLoginBean;
import com.ymatou.app.api.parameter.GroupCreateBean;
import com.ymatou.app.api.parameter.GroupJoinBean;
import com.ymatou.app.api.parameter.TopicProductDetailsBean;
import com.ymatou.app.api.service.AppLoginCall;
import com.ymatou.app.api.service.GroupCreateCall;
import com.ymatou.app.api.service.GroupJoinCall;
import com.ymatou.app.api.service.TopicProductDetailsCall;

public class AppCallService {

	/**
	 * 创建团
	 * 
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> createGroup(String UserName, String PassWord) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		GroupCreateBean groupcreatebean = new GroupCreateBean();
		GroupCreateCall groupcreatecall = new GroupCreateCall();
		try {
			Map user = appLogin(UserName, PassWord);
			groupcreatebean.setAccessToken((String) user.get("AccessToken"));// 登陆token
			int TopicId = 1;
			groupcreatebean.setTopicId(1);// 主题ID
			groupcreatecall.setData(false,groupcreatebean);
			groupcreatecall.callService();
			map.put("GroupCode", groupcreatecall.getGroupCode());
			map.put("TopicId", TopicId);
			map.put("AccessToken", user.get("AccessToken"));
		} catch (Exception e) {
			// e.printStackTrace();
		}
		return map;
	}

	/**
	 * 输入用户名密码登陆
	 * 
	 * @param 用户名
	 *            、密码
	 * @return Map
	 */
	public Map<String, Comparable> appLogin(String UserName, String Password) {
		Map<String, Comparable> map = new HashMap<String, Comparable>();
		AppLoginBean apploginbean = new AppLoginBean();
		AppLoginCall applogincall = new AppLoginCall();
		apploginbean.setUserName(UserName);
		apploginbean.setPassword(Password);
		applogincall.setData(false,apploginbean);
		try {
			applogincall.callService();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String AccessToken = applogincall.getAccessToken();
		int UserId = applogincall.getUserId();
		map.put("UserId", UserId);
		map.put("AccessToken", AccessToken);
		return map;
	}

	/**
	 * 输入用户名密码参加团
	 */
	public void GroupJoin(String UserName, String Password, int TopicId, String GroupCode) {
		GroupJoinBean groupjoinbean = new GroupJoinBean();
		GroupJoinCall groupjoincall = new GroupJoinCall();
		Map map = appLogin(UserName, Password);
		groupjoinbean.setGroupCode(GroupCode);
		groupjoinbean.setTopicId(TopicId);
		groupjoinbean.setAccessToken((String) map.get("AccessToken"));
		groupjoincall.setData(false,groupjoinbean);
		try {
			groupjoincall.callService();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 根据主题Id查询团产品信息
	 * 
	 * @return
	 */
	public Map<String, Comparable> getTopicProductDetails(int TopicId) {
		TopicProductDetailsBean topicproductdetailsbean = new TopicProductDetailsBean();
		TopicProductDetailsCall topicproductdetailscall = new TopicProductDetailsCall();

		topicproductdetailsbean.setTopicId(TopicId);
		Map<String, Comparable> map = new AppCallService().appLogin("13577776666", "123456");
		topicproductdetailsbean.setAccessToken((String) map.get("AccessToken"));
		topicproductdetailscall.setData(false,topicproductdetailsbean);
		try {
			topicproductdetailscall.callService();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		map.put("CatalogId", topicproductdetailscall.getCatalogId());
		map.put("SellerId", topicproductdetailscall.getSellerId());
		// Logger.debug(topicproductdetailscall.getReturnData());
		return map;
	}
}
